const { shopAuth, userAuth } = require('../models/index')
const errorHelper = require('../util/errorHelper')

class AuthController {
  constructor() {
    // empty
  }
  async createAuth({ type, uuId, identityType, identifier, credential, disable = 0 }) {
    const authRec = {
      identityType,
      identifier,
      credential,
      disable
    }
    let newAuth = null
    if (type === 'shop') {
      authRec.shopId = uuId
      newAuth = await shopAuth.create(authRec)
    } else {
      authRec.userId = uuId
      newAuth = await userAuth.create(authRec)
    }
    return newAuth
  }

  async isAuthExist({ type, where, include }) {
    const auth = type === 'shop' ? shopAuth : userAuth
    const targetAuth = await auth.findAll({
      where,
      include
    })
    return targetAuth
  }
}

module.exports = new AuthController()
